// pages/login/login.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign:true,//true为登陆，false为注册
    role:true,//true为租户，false为经纪人
    phone:'',
    iDcard:'',
    name:'',
    yzm:'',
    password:'',
    confirmpassword:''
  },
  //点击注册
  sign:function(){
    const that = this;
    that.setData({
      sign:false
    });
    this.roleone();
  },
  //点击登录
  login: function () {
    const that = this;
    that.setData({
      sign: true
    });
    this.roleone();
  },
  //点击租户事件
  roleone:function(){
    const that = this;
    that.setData({
      role : true,
      phone: '',
      iDcard: '',
      name: '',
      yzm: '',
      password:'',
      confirmpassword:''
    })
  },
  //点击经纪人事件
  roletwo:function(){
    const that = this;
    that.setData({
      role : false,
      phone: '',
      iDcard: '',
      yzm: '',
      password: '',
      confirmpassword:''
    })
  },
  //实时获取手机号
  bindPhoneInput:function(e){
    var that = this;
    that.setData({
      phone: e.detail.value
    })
  },
  //实时获取密码
  bindPasswordInput: function (e) {
    var that = this;
    that.setData({
      password: e.detail.value
    })
  },
  //实时获取验证码
  bindYzmInput: function (e) {
    var that = this;
    that.setData({
      yzm: e.detail.value
    })
  },
  //实时获取身份证
  bindIdcardInput: function (e) {
    var that = this;
    that.setData({
      iDcard: e.detail.value
    })
  },
  //实时获取确认验证码
  bindComfirmpasswoedInput: function (e) {
    var that = this;
    that.setData({
      confirmpassword: e.detail.value
    })
  },
  //获取验证码事件
  getCode:function(){
    //验证手机号是否正确
    const phonenumber = this.data.phone;
    if (!(/^1[34578]\d{9}$/.test(phonenumber)) || phonenumber.length !== 11) {
      wx.showToast({
        title: '手机号有误,请重新填写', 
        icon: 'none',
        duration: 2000
      })
    }else{
      //调用后台获取验证码接口
      console.log('手机验证成功，调用接口获取验证码')
    }
  },
  //提交登录表单事件
  loginbtn: function () {
    // const name = e.detail.value.name;
    const phone = this.data.phone;
    const yzm = this.data.yzm;
    const password = this.data.password;
    console.log('form发生了submit事件，携带数据为：', phone, yzm, password)
    // 分角色判断
    if(this.data.role){
      //租户
      if (!( phone & yzm & password)) {
        wx.showToast({
          title: '请填写完整信息111',
          icon: 'none',
          duration: 2000
        })
      } else {

        //判断2次密码是否一样
        // if (password !== confirmpassword) {
        //   wx.showToast({
        //     title: '两次输入密码不一致',
        //     icon: 'none',
        //     duration: 2000
        //   })
        // }else{
          //调用后台登录接口

          //跳转到个人中心页面
        wx.switchTab({
            url: '/pages/user/user',
          });
          //个人信息传给后台，现在暂时先写死
        app.globalData.userInfo = {
          name: "张杨柳",
          phone: "15088888888",
          password: "123456",
          type: "normal",
          idCard: "420888888888888888"
        }
        //页面复原
        this.login();
        // }
        
      }
    }else{
      const iDcard = this.data.yzm;
      if (!(phone & yzm & iDcard & password)) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none',
          duration: 2000
        })
        
      } else {
        //判断2次密码是否一样
        // if (password !== confirmpassword) {
        //   wx.showToast({
        //     title: '两次输入密码不一致',
        //     icon: 'none',
        //     duration: 2000
        //   })
        // }else{
          //调用后台登录接口
          //跳转到个人中心页面
        wx.switchTab({
            url: '/pages/user/user',
          });
        //页面复原
        this.login();
        // }
        

      }
    }
   
  },
  //提交表单注册
  signbtn:function(){
    const phone = this.data.phone;
    const yzm = this.data.yzm;
    const password = this.data.password;
    const confirmpassword = this.data.confirmpassword;
    // 分角色判断
    if (this.data.role) {
      //租户
      console.log(phone, yzm, password, confirmpassword);
      if (!(phone & yzm & password & confirmpassword)) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none',
          duration: 2000
        })
        
      } else {
        //判断2次密码是否一样
        if (password !== confirmpassword) {
          wx.showToast({
            title: '两次输入密码不一致',
            icon: 'none',
            duration: 2000
          })
        }else{
          //调用后台注册接口

          //跳转到个人中心页面
          wx.switchTab({
            url: '/pages/user/user',
          });
          //个人信息传给后台，现在暂时先写死
          app.globalData.userInfo = {
            name: "张杨柳",
            phone: "15088888888",
            password: "123456",
            type: "normal",
            idCard: "420888888888888888"
          }
          //页面复原
          this.login();
        }
       
      }
    } else {
     
      const iDcard = this.data.iDcard;
      console.log(phone, yzm, password, confirmpassword, iDcard);
      if (!(phone & yzm & iDcard & password & confirmpassword)) {
        wx.showToast({
          title: '请填写完整信息',
          icon: 'none',
          duration: 2000
        })
        
      } else {
        //判断2次密码是否一样
        if (password !== confirmpassword) {
          wx.showToast({
            title: '两次输入密码不一致',
            icon: 'none',
            duration: 2000
          })
        }else{
          //调用后台注册接口
          //跳转到个人中心页面
          wx.switchTab({
            url: '/pages/user/user',
          });
          //页面复原
          this.login();
        }
       

      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})